package com.springboot.shanhexin.controller;

import com.springboot.shanhexin.entity.Page;
import com.springboot.shanhexin.entity.place;
import com.springboot.shanhexin.service.placeService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class placeCtrl {

    @Autowired
    private placeService placeService;

    //分页显示全部
    @RequestMapping("/place")
    public String place(Model model, String pageNow, String nosearch) {
        int totalCount = 0;
        totalCount = placeService.getCount();
        List<place> placeList=new ArrayList<place>();
        Page page=null;
        if(pageNow!=null){
            page=new Page(totalCount, Integer.parseInt(pageNow));
            placeList=placeService.findByPage(page.getStartPos(), page.getPageSize());
        }else{
            page=new Page(totalCount, 1);//初始化pageNow为1
            placeList=placeService.findByPage(page.getStartPos(), page.getPageSize());
        }
        model.addAttribute("page", page);
        model.addAttribute("top", "top_place");
        model.addAttribute("show_left", "left_place");
        model.addAttribute("left", "left_y_place");
        model.addAttribute("mypage", "place");
        model.addAttribute("place", placeList);
        if(nosearch != null){
            model.addAttribute("nosearch", nosearch);
        }
        return "houtai_place";
    }

    //分页显示全部搜索
    @RequestMapping("/searchplace")
    public String searchplace(Model model, String pageNow,int types,String keyword) {
        int totalCount = 0;
        String keyword_new="b.name";
        if(types == 1) {
            keyword_new = "b.name";
        }else if (types == 2) {
            keyword_new = "a.name";
        }
        totalCount = placeService.getsearchCount(keyword_new,keyword);
        List<place> placeList=new ArrayList<place>();
        Page page=null;
        if(pageNow!=null){
            page=new Page(totalCount, Integer.parseInt(pageNow));
            placeList=placeService.findsearchByPage(keyword_new,keyword,page.getStartPos(), page.getPageSize());
        }else{
            page=new Page(totalCount, 1);//初始化pageNow为1
            placeList=placeService.findsearchByPage(keyword_new,keyword,page.getStartPos(), page.getPageSize());
        }
        if(placeList != null && totalCount != 0){
            model.addAttribute("page", page);
            model.addAttribute("top", "top_place");
            model.addAttribute("show_left", "left_place");
            model.addAttribute("left", "left_y_place");
            model.addAttribute("mypage", "search&types="+types+"&keyword="+keyword+"");
            model.addAttribute("place", placeList);
            return "houtai_place";
        }else{
            model.addAttribute("nosearch", "nosearch");
            return "redirect:/place?nosearch=nosearch";
        }
    }

    //添加
    //接收并返回json格式数据
    @RequestMapping("/addplace")
    @ResponseBody
    public String addplace(place place) {
        placeService.add(place);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", "1");
        return jsonObject.toString();
    }

    //更改
    @RequestMapping("/updateplace")
    public String updateplace(place place) {
        placeService.update(place);
        return "redirect:/place";
    }

    //删除
    @RequestMapping("/deleteplace")
    public String deleteplace(int userid) {
        placeService.delete(userid);
        return "redirect:/place";
    }

    //跳转到更改页面
    @RequestMapping("/to_updateplace")
    public String to_updateplace(Model model, int userid) {
        List<place> placeList=new ArrayList<place>();
        placeList = placeService.getUserone(userid);
        model.addAttribute("top", "top_place");
        model.addAttribute("show_left", "left_updateplace");
        model.addAttribute("left", "left_y_place");
        model.addAttribute("mypage", "place");
        model.addAttribute("place", placeList);
        return "houtai_updateplace";
    }

    //跳转到添加页面
    @RequestMapping("/to_addplace")
    public String to_addplace(Model model) {
        model.addAttribute("top", "top_place");
        model.addAttribute("show_left", "left_addplace");
        model.addAttribute("left", "left_y_place");
        model.addAttribute("mypage", "place");
        return "houtai_addplace";
    }

}
